"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectProjectjsCtrl
 * @description
 * # ProjectProjectjsCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectProjectjsCtrl", function(
    $scope,
    $rootScope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);

    $scope.chooseProject = function() {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.chooseContract = function() {
      var params = $scope.param;
      if (!params.projectId || !params.contractType) {
        Util.Toast("请先选择项目与合同类型");
        return;
      }
      var projectParams = {
        formKey: params.contractType,
        fieldName: "contractNo",
        projectId: params.projectId
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseSupplier = function() {
      var params = $scope.param;
      var projectParams = {
        formKey: "Xm_SupplierStore",
        fieldName: "supplierNo"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.showPicker = function() {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
    };
    $scope.deletedItem = function(index) {
      $scope.param.details.splice(index, 1);
      $scope.curindex = -1;
    };

    $scope.addItem = function() {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      var data=Object.assign({},$scope.picker);
      if(data.startTime){
        data.startTime=data.startTime+" 00:00:00"
      }
      if(data.endTime){
        data.endTime=data.endTime+" 00:00:00"
      }
      $scope.param.details.push(data);
      // 隐藏表单
      closePicker();
    };
    var closePicker = function() {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function() {
      closePicker();
    });
    $scope.closePicker = function() {
      closePicker();
    };
    $scope.swipe = function(index, type) {
      if (type) {
        $scope.payindex = index;
      } else {
        $scope.curindex = index;
      }
    };
    $scope.resetlist = function(type) {
      if (type) {
        $scope.payindex = -1;
      } else {
        $scope.curindex = -1;
      }
    };
    // 验证是否输入完整
    var checkPicker = function(data) {
      if (!data.name) {
        Util.Toast("请输入名称");
        return;
      }
      if (!data.quantity) {
        Util.Toast("请输入数量");
        return;
      }
      if (!data.totalPrice) {
        Util.Toast("请输入总价");
        return;
      }
      return true;
    };

    $scope.submit = function(operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectsettlement;
      options.param = angular.copy($scope.param, {});
      options.param.detailJson = JSON.stringify(options.param.details);
      delete options.param.details;
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if ($scope.param.details.length < 1) {
        Util.Toast("请添加结算明细");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.details = $scope.param.details || [];
      });
    };
    // 初始化
    init();
  });
